import { defineStore } from 'pinia'
import { store } from '../index'
// @ts-ignore
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
const { wsCache } = useCache('sessionStorage')
import {CheckTypeApi} from "@/api/ecg/checktype";

export interface CheckTypeSimpleVO {
  value: number
  name: string
  displayBarcode: []
  notes: string
  seqPrefix: string // 序号前缀
  expenseRecognition: number
}
export interface CheckTypeState {
  checkTypeMap: Map<number, CheckTypeSimpleVO>
  isSetCheckType: boolean
}

export const useCheckTypeStore = defineStore('checktype ', {
  state: (): CheckTypeState => ({
    checkTypeMap: new Map<number, CheckTypeSimpleVO>(),
    isSetCheckType: false
  }),
  getters: {
    getCheckTypeMap(): Recordable {
      const checkTypeMap = wsCache.get(CACHE_KEY.CHECKTYPE_CACHE)
      if (checkTypeMap) {
        this.checkTypeMap = checkTypeMap
      }
      return this.checkTypeMap
    },
    getIsSetCheckType(): boolean {
      return this.isSetCheckType
    }
  },
  actions: {
    async setCheckTypeMap() {
      const checkTypeMap = wsCache.get(CACHE_KEY.CHECKTYPE_CACHE)
      if (checkTypeMap) {
        this.checkTypeMap = checkTypeMap
        this.isSetCheckType = true
      } else {
        const res = await CheckTypeApi.getSimpleCheckTypeList()
        // 设置数据
        const checkTypeMap = new Map<number, CheckTypeSimpleVO>()
        res.forEach((simpleVO: CheckTypeSimpleVO) => {
          // 获得 dictType 层级
          const enumValueObj = checkTypeMap[simpleVO.value]
          if (!enumValueObj) {
            checkTypeMap[simpleVO.value] = simpleVO
          }
        })
        this.checkTypeMap = checkTypeMap
        this.isSetCheckType = true
        wsCache.set(CACHE_KEY.CHECKTYPE_CACHE, checkTypeMap, { exp: 60 }) // 60 秒 过期
      }
    },
    getCheckTypeName(type: number) {
      if (!this.isSetCheckType) {
        this.setCheckTypeMap()
      }
      return this.checkTypeMap[type].name
    },
    getCheckTypeOptions() {
      if (!this.isSetCheckType) {
        this.setCheckTypeMap()
      }
      return Object.keys(this.checkTypeMap).map((key) => {
        return {
          value: parseInt(key, 10),
          label: this.checkTypeMap[key].name
        }
      });
    },
    getCheckTypeDispBarCode(type: number) {
      if (!this.isSetCheckType) {
        this.setCheckTypeMap()
      }
      return this.checkTypeMap[type].displayBarcode
    },
    getCheckTypeNotes(type: number) {
      if (!this.isSetCheckType) {
        this.setCheckTypeMap()
      }
      return this.checkTypeMap[type].notes
    },
    getCheckTypeSeqPrefix(type: number) {
      if (!this.isSetCheckType) {
        this.setCheckTypeMap()
      }
      return this.checkTypeMap[type].seqPrefix
    },
    getExpenseRecognition(type: number) {
      if (!this.isSetCheckType) {
        this.setCheckTypeMap()
      }
      return this.checkTypeMap[type].expenseRecognition
    },
    async resetCheckTypeInfo() {
      wsCache.delete(CACHE_KEY.CHECKTYPE_CACHE)
      const res = await CheckTypeApi.getSimpleCheckTypeList()
      // 设置数据
      const checkTypeMap = new Map<number, CheckTypeSimpleVO>()
      res.forEach((simpleVO: CheckTypeSimpleVO) => {
        // 获得 dictType 层级
        const enumValueObj = checkTypeMap[simpleVO.value]
        if (!enumValueObj) {
          checkTypeMap[simpleVO.value] = simpleVO
        }
      })
      this.checkTypeMap = checkTypeMap
      this.isSetCheckType = true
      wsCache.set(CACHE_KEY.CHECKTYPE_CACHE, checkTypeMap, { exp: 60 }) // 60 秒 过期
    }
  }
})

export const useCheckTypeStoreWithOut = () => {
  return useCheckTypeStore(store)
}
